//package com.ld.security.gateway.filter;
//
//
//import org.springframework.beans.wrapper.annotation.Autowired;
//import org.springframework.cloud.client.discovery.DiscoveryClient;
//import org.springframework.cloud.client.loadbalancer.LoadBalancerClient;
//import org.springframework.cloud.gateway.filter.GatewayFilter;
//import org.springframework.cloud.gateway.filter.GatewayFilterChain;
//import org.springframework.cloud.gateway.route.Route;
//import org.springframework.cloud.gateway.support.ServerWebExchangeUtils;
//import org.springframework.core.Ordered;
//import org.springframework.http.server.reactive.ServerHttpRequest;
//import org.springframework.stereotype.Component;
//import org.springframework.web.server.ServerWebExchange;
//import org.springframework.web.util.UriComponentsBuilder;
//import reactor.core.publisher.Mono;
//
//import java.net.URI;
//import java.util.List;
//
//
//@Component
//public class NgFilter implements GatewayFilter,Ordered {
//
//    @Autowired
//    private LoadBalancerClient loadBalancer;
//    @Autowired
//    private DiscoveryClient discoveryClient;
//
//    private String getServer(String ng){
//        List<String> list =  discoveryClient.getServices();
//        String serverName = "";
//        for (String s : list) {
//            if(s.indexOf(ng) != -1){
//                serverName = s.toUpperCase() ;
//            }
//        }
//        return  serverName;
//
//    }
//
//
//    @Override
//    public Mono<Void> filter(ServerWebExchange exchange, GatewayFilterChain chain) {
//
//
//        ServerHttpRequest request =  exchange.getRequest();
//        try{
//
//                String path = request.getURI().getPath();
//                ListString listString =  new ListString(path);
//                listString.remove(0);
//
//                String ng = request.getQueryParams().getFirst("ng");
//                if(ng == null){
//                    return chain.filter(exchange);
//                }
//
//                String serverName = getServer(ng);
//                Route route = (Route)exchange.getAttribute(ServerWebExchangeUtils.GATEWAY_ROUTE_ATTR);
//
//                URI uri = request.getURI();
//
//                String url =  uri.getScheme()+"://"+uri.getAuthority()+serverName+listString.getValue();
//
//                URI requestUrl =UriComponentsBuilder.fromHttpUrl(url).build().toUri();
//                ServerHttpRequest newReq = request.mutate().uri(requestUrl).build();
//
//                Route newRoute=   Route.async()
//                        .asyncPredicate(route.getPredicate())
//                        .filters(route.getFilters())
//                        .id(route.getId())
//                        .order(route.getOrder())
//                        .uri(requestUrl)
//                        .build();
//                exchange.getAttributes().put(ServerWebExchangeUtils.GATEWAY_ROUTE_ATTR,newRoute);
//
//                return chain.filter(exchange.mutate().request(newReq).build());
//
//
//        }catch (Exception e){
//            e.printStackTrace();
//        }
//
//        return chain.filter(exchange);
//    }
//
//
//
//    @Override
//    public int getOrder() {
//        return 100;
//    }
//}
